<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="云盘名称" prop="netDiskName" maxlength="50">
				<el-input v-model="form.netDiskName" placeholder="云盘名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="服务器目录" prop="rootPath">
				<el-input v-model="form.rootPath" placeholder="服务器目录" clearable></el-input>
			</el-form-item>
			<el-form-item label="硬盘容量" prop="spaceLimit">
				<el-input type="number" v-model="form.spaceLimit" placeholder="硬盘容量 单位G" style="width: 100%;">
					<template v-slot:append="">G</template>
				</el-input>
			</el-form-item>
			<el-form-item label="文件排序类型" prop="orderBy">
				<el-select v-model="form.orderBy" style="width: 100%">
					<el-option v-for="item in sortTypeList" :key="item.value" :value="item.value" :label="item.label"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="云盘说明">
				<el-input type="textarea" :rows="2" maxlength="100" placeholder="云盘说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '创建云盘',
				edit: '修改云盘'
			},
			sortTypeList: [
				{value: "0", label: "文件名称"},
				{value: "1", label: "修改时间"},
				{value: "2", label: "文件大小"},
			],
			form: {
				netDiskId: "",
				netDiskName: "",
				sortNo: "",
				rootPath: "",
				spaceLimit: "",
				orderBy: "1",
				status:'1',
				remark: ''
			},
			rules: {
				netDiskName: [
					{required: true, message: '云盘名称不能为空'}
				],
				rootPath: [
					{required: true, message: '服务器目录不能为空'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if (this.mode == "add") {
						this.isSaveing = true;
						res = await this.$API.file.netDisk.insert.post(this.form);
						this.isSaveing = false;
					} else if (this.mode == "edit") {
						this.isSaveing = true;
						res = await this.$API.file.netDisk.update.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.netDiskId = data.netDiskId
			this.form.netDiskName = data.netDiskName
			this.form.sortNo = data.sortNo
			this.form.rootPath = data.rootPath
			this.form.spaceLimit = data.spaceLimit
			this.form.orderBy = data.orderBy
			this.form.status = data.status
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
